<template>
  <BasicModal :show="show" :title="'修改' + title" :widthComfirm="true" @comfirm="comfirm" @closeModal="show = false">
    <template #content>
      <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ style: { width: '100px' } }">
        <a-form-item :label="title" name="hr">
          <a-select v-model:value="form.hr" placeholder="请选择HR" show-search :filter-option="filterOption">
            <a-select-option :value="item.value" v-for="item in HRList" :key="item.label">{{ item.label
              }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="面试会议链接" name="interviewUrl" v-if="rowName == '面试官'">
          <a-input v-model:value="form.interviewUrl" placeholder="面试会议链接"></a-input>
        </a-form-item>
      </a-form>
      <div class="title">
        <span></span>
        更换记录
      </div>
      <div class="record">
        <div class="record-item" v-for="item in recordList" :key="item.label" v-if="recordList.length > 0">
          <div class="top">
            <div class="label">{{ item.type }}</div>
            <div class="time">
              <i class="iconfont icon-shijian"></i>
              <span>{{ item.createTime }}</span>
            </div>
          </div>
          <div class="bottom">
            <div class="change">
              <div class="before">{{ item.oldResource }}</div>
              <div>修改后</div>
              <div class="after">{{ item.newResource }}</div>
            </div>
            <div class="opeartor">
              操作人：{{ item.operator }}
            </div>
          </div>
        </div>
        <a-empty v-else></a-empty>
      </div>
    </template>
  </BasicModal>
</template>

<script setup lang='ts'>
import { ref, watch, nextTick } from "vue"
import BasicModal from './BasicModal.vue';
import { selectUserByRole, selectResumesLogList, preserveUser, interviewUser, interviewer } from "@/api/application-details/index.ts";
import type { Rule } from 'ant-design-vue/es/form';
import { message } from "ant-design-vue";
const show = ref<boolean>(false);
import { comStores } from '@/store/common/comStore';
const store = comStores()
const form = ref({
  hr: "",
  interviewUrl: "",
})
const rules: Record<string, Rule[]> = {
  hr: [{ required: true, message: '请选择', trigger: 'change' },
  ],

};
const recordList = ref<any>([
])

const title = ref<string>("");
const filterOption = (input: string, option: any) => {
  return option.key.toLowerCase().indexOf(input.toLowerCase()) >= 0;
};

const HRList = ref<{ label: string; value: string }[]>([]);
const formRef = ref<any>(null)
const comfirm = async () => {
  const res = await formRef.value.validate();
  if (!res) return;
  const { resumeId, } = store.currentResume
  console.log(HRList.value, 'HRList')
  let cur = HRList.value.find(el => el.value == form.value.hr);
  console.log(cur, 'cur')
  if (!cur) return message.error("请选择HR")
  const text = form.value.interviewUrl || ''
  const regex = /(https?:\/\/[^\s#]+)/;
  const match = text?.match(regex);
  let param: any = {
    userId: form.value.hr,
    userName: cur?.label,
    operator: store.userInFor.userName,
    interviewUrl: match ? match[0] : '',
    resumeInfos: [{
      id: resumeId,
      name: rowHr.value
    }]
  };
  let data: any = {}
  if (rowName.value == '约面HR') {
    data = await interviewUser(param)
  } else if (rowName.value == '面试官') {
    data = await interviewer(param)
  } else {
    data = await preserveUser(param)
  }
  console.log(data, 'daat')
  if (data && data.code === '10000') {
    message.success("修改成功");
    show.value = false
    await store.getResumeInfo(resumeId);
  } else {
    message.error(data.msg)
  }
};
const rowName = ref<string>("");
const rowHr = ref<string>("")
const showDialog = async (hr, name, item) => {
  console.log(hr, 'hr')

  show.value = true;
  rowHr.value = hr
  rowName.value = name;
  title.value = name
  if (item) {
    form.value.interviewUrl = item.interviewUrl
    console.log(form.value, 'form.value')
  }
  let param = "HR"
  if (name == '面试官') {
    param = "INTERVIEW"
  }
  const data = await selectUserByRole(param, 0);
  console.log(data)
  if (data && data.code === '10000') {
    HRList.value = data.data
    let trueHr = data.data.find(el => el.label == hr || el.value == hr);
    form.value.hr = trueHr ? trueHr.value : ""
  } else {
    message.error(data.msg)
  }
  getRecord();
}

const getRecord = async () => {
  let map = {
    "约面HR": 1,
    "维护HR": 2,
    "面试官": 3,
  }
  let logType = map[rowName.value]
  const { resumeId } = store.currentResume
  const data = await selectResumesLogList({
    resumeId, logType: logType
  });
  recordList.value = data.data
}
defineExpose({ show, showDialog })
</script>

<style lang="less" scoped>
.record {
  max-height: 40vh;
  overflow: auto;
}

.record-item {
  width: 100%;
  background: #F5F7FB;
  border-radius: 10px 10px 10px 10px;
  margin-bottom: 20px;
  padding: 17px 20px;
  box-sizing: border-box;
  position: relative;

  .top {
    display: flex;
    align-items: center;
    justify-content: space-between;

    .label {
      font-weight: 500;
      font-size: 20px;
      color: #333333;
      line-height: 23px;

    }

    .time {
      display: flex;
      align-items: center;
      gap: 5px;
      color: #abbacd;
    }
  }

  .bottom {
    display: flex;
    align-items: center;
    margin-top: 17px;

    .change {
      display: flex;
      gap: 30px;
      font-size: 18px;
      color: #333;

      .before {
        text-decoration: line-through;
        color: #ABBACD;
      }
    }

    .add {
      p {
        color: #333;
        line-height: 31px;
        font-size: 18px;
        white-space: pre-line
      }
    }
  }

  .opeartor {
    position: absolute;
    right: 20px;
    bottom: 8px;
    color: #666;

  }

}

.title {

  font-weight: 500;
  font-size: 18px;
  color: #333333;
  line-height: 21px;
  display: flex;
  align-items: center;
  margin-bottom: 10px;

  span {
    width: 4px;
    height: 24px;
    background: #209EE4;
    border-radius: 2px 0 0 2px;
    margin-right: 8px;
  }
}
</style>